package apiUtils.apis;

import android.text.TextUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import apiUtils.APISHelper;
import apiUtils.CallBack;
import apiUtils.models.PayAppInfo;
import apiUtils.models.PayExceptionType;
import apiUtils.params.AESParams;
import apiUtils.params.Params;
import appUtils.LogUtil;
import appUtils.PayAESUtil;
import appUtils.PayMD5Util;
import appUtils.ToastUtil;
import io.reactivex.functions.Consumer;
import okhttp3.Call;

import static constant.ApiConstants.TASK_SUCCESS;

/**
 * Created by Administrator on 2017/8/23.
 */

public class GetAppInfoAPI extends APISHelper {
    private static final String url = "/services/getServerLogin/login";
    private static final String tag = "GetAppInfoAPI";
    //账号
    protected String mUserName;
    //密码
    protected String mPassWord;

    public void getAppInfoAPI(String username, String password, final Listener listener) {
        mUserName = username;
        mPassWord = password;
        try {
            get(tag, url, new CallBack() {
                @Override
                public void onSuccess(Call call, String response) throws IOException {
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        if (TASK_SUCCESS.equals(jsonObject.getString("flag"))) {
                            ArrayList<PayAppInfo> appInfos = new ArrayList<>();
                            final String msg = PayAESUtil.decryptAES(tag, jsonObject.getString("msg"));
                            if (TextUtils.isEmpty(msg)) {
                                failure(-1, PayExceptionType.DECRYPT_EXCEPTION.getMessage(), listener);
                                return;
                            }
                            LogUtil.log("getLogin decrypt info = " + msg);
                            if (isJsonArray(msg)) {
                                JSONArray jsonArray = new JSONArray(msg);
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    PayAppInfo appInfo = new PayAppInfo();
                                    JSONObject jsonObjectitem = jsonArray.getJSONObject(i);
                                    appInfo.setAppName(jsonObjectitem.getString("appName"));
                                    appInfo.setAppid(jsonObjectitem.getString("appid"));
                                    appInfo.setAppKey(jsonObjectitem.getString("appKey"));
                                    appInfo.setAppAESKey(jsonObjectitem.getString("appSdkKey"));
                                    appInfo.setAppAesVector(jsonObjectitem.getString("appAesXl"));
                                    appInfos.add(appInfo);
                                }
                                success(appInfos, listener);
                            } else {
                                LogUtil.log("doGetPlatformList flag exception");
                                failure(-1, PayExceptionType.RETURN_ERROR_DATA.getMessage(), listener);
                                ToastUtil.showShort(PayExceptionType.RETURN_ERROR_DATA.ordinal());
                            }
                        } else {
                            failure(-1, "", listener);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        failure(-1, e.getMessage(), listener);
                    }
                }

                @Override
                public void onError(int code) {
                    failure(code, "", listener);
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
            failure(-1, e.getMessage(), listener);
        }
    }

    private void success(final List<PayAppInfo> payAppInfoList, final Listener listener) {
        subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {
                if (null != listener) {
                    listener.onSuccess(payAppInfoList);
                }
            }
        });
    }

    private void failure(final int code, final String msg, final Listener listener) {
        subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {
                if (listener != null) {
                    listener.onFailure(code, msg);
                }
            }
        });
    }

    @Override
    protected Params initParams() throws JSONException {
        String passwordM = PayMD5Util.getMD5(mPassWord).toLowerCase();
        AESParams params = new AESParams();
        params.put("loginName", mUserName);
        params.put("password", passwordM);
        return params;
    }

    public interface Listener {
        void onSuccess(List<PayAppInfo> platFormModelList);

        void onFailure(int code, String message);
    }
}
